<?php

namespace base;

use yii\base\Widget;
use yii\helpers\Html;

class BaseWidget extends Widget
{
	public $options;
	
	public $function;
	
	public $name;
	
	public $viewFile = '@submodule/widgets/text/views/_form';
	
	public $viewPath = '';

	
	
	public function run()
    {
		
		$viewFile = "@submodule/widgets/{$this->viewPath}/views/_form";
        return $this->render($viewFile, self::widgetField());
    }
	
	/*public static function render($viewFile){
		$this->viewFile = $viewFile;
		$this->run();
		
	}*/
	
	
	/*
	 * 模型
	 * @param
	 * @return false
	 * @author xiongyouliang
	*/
	public function widgetField(){
	
		$return = $this->options;
		$primarykey = isset($this->options['primarykey']) ? $this->options['primarykey'] : 'id';
		
		$return['name'] = isset($this->options['name']) ? $this->options['name'] : "Models[{$this->options['id']}]";
		if (isset($this->options['value']) && $this->options['value']){
			$return['value'] = $this->options['value'];
		}else if (isset($this->options['attributes'][$primarykey]) && $this->options['attributes'][$primarykey]){
			$return['value'] = isset($this->options['attributes'][$this->options['id']]) ? $this->options['attributes'][$this->options['id']] : '';
			$viewfunction = $this->options['id'].'Update';
			//echo (int)method_exists($this->function, 'passwordUpdate');
			if (method_exists($this->function, $viewfunction)){
				$return['value'] = $this->function->$viewfunction($this->options['fields']['fieldid'], $this->options['id'], $this->options['attributes']);
			}
		}else{
			$return['value'] = @$this->options['fields']['setting']['defaultvalue'];
		}
		
        return $return;
		
	}
	
}



